package com.jueyue.dao.template;

import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import com.jueyue.dao.spring.rowMapper.MiniColumnMapRowMapper;
import com.jueyue.dao.util.PublicUtil;

/**
 * jdbc 整合类@author jueyue 调用些常用方法
 */
public class MiniDaoTemplate extends JdbcTemplate {

	private NamedParameterJdbcTemplate nameTemplate;

	public MiniDaoTemplate(DataSource dataSource) {
		super(dataSource);
		nameTemplate = new NamedParameterJdbcTemplate(dataSource);
	}

	public List<Map<String, Object>> queryForList(String sql) {
		return query(sql, new MiniColumnMapRowMapper());
	}

	public <T> List<T> queryForList(String sql, Class<T> object) {
		if (PublicUtil.isWrapClass(object)) {
			return super.queryForList(sql, object);
		}
		return query(sql,
				ParameterizedBeanPropertyRowMapper.newInstance(object));
	}

	public <T> List<T> queryForList(String sql, Object[] args, Class<T> object) {
		if (PublicUtil.isWrapClass(object)) {
			return super.queryForList(sql, args, object);
		}
		return query(sql, args,
				ParameterizedBeanPropertyRowMapper.newInstance(object));
	}

	public Map<String, Object> queryForMap(String sql) {
		return queryForObject(sql, new MiniColumnMapRowMapper());
	}

	public <T> T queryForObject(String sql, Object[] args, Class<T> object) {
		if (PublicUtil.isWrapClass(object)) {
			return super.queryForObject(sql, args, object);
		}
		return queryForObject(sql, args,
				ParameterizedBeanPropertyRowMapper.newInstance(object));
	}

	public <T> List<T> queryForList(String sql, Map<String, Object> map,
			Class<T> object) {
		if (PublicUtil.isWrapClass(object)) {
			return nameTemplate.queryForList(sql, map, object);
		}
		return nameTemplate.query(sql, map,
				ParameterizedBeanPropertyRowMapper.newInstance(object));
	}

	public List<Map<String, Object>> queryForList(String sql,
			Map<String, Object> map) {
		return nameTemplate.queryForList(sql, map);
	}

}
